package com.xjl;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * @Author：xjl
 * @Date：2024/1/1 16:12
 */
public class OptionalDemo {


    public static void main(String[] args) {
//        Author author = getAuthor();
//        Optional<Author> authorOptional = Optional.ofNullable(author);
//        authorOptional.ifPresent(author1 -> System.out.println(author1.getName()));
//
//        Optional<Author> authorOptional = getAuthorOptional();
//        authorOptional.ifPresent(author1 -> System.out.println(author1.getName()));

//        Author author = getAuthor();
//        Optional<Author> authorOptional = Optional.of(author);
//        authorOptional.ifPresent(author1 -> System.out.println(author1.getName()));

//        Optional<Author> authorOptional = getAuthorOptional();
//        Author author = authorOptional.orElseGet(() -> new Author());//如果有值返回值，没有值返回指定的东西
//        System.out.println(author.getName());


//        Optional<Author> authorOptional = getAuthorOptional();//如果有值返回值，没有值返回指定异常
//        try {
//            Author author = authorOptional.orElseThrow(() -> new RuntimeException("数据为null"));
//            System.out.println(author);
//        } catch (Throwable e) {
//            throw new RuntimeException(e);
//        }
//        testFilter();
//        testIsPresent();
        testMap();
    }

    private static void testMap() {
        Optional<Author> authorOptional = getAuthorOptional();
        Optional<List<Book>> optionalBooks = authorOptional.map(new Function<Author, List<Book>>() {
            @Override
            public List<Book> apply(Author author) {
                return author.getBooks();
            }
        });
        optionalBooks.ifPresent(new Consumer<List<Book>>() {
            @Override
            public void accept(List<Book> books) {
                System.out.println(books);
            }
        });
    }

    private static void testIsPresent() {
        Optional<Author> authorOptional = getAuthorOptional();
        if (authorOptional.isPresent()){
            System.out.println(authorOptional.get().getName());
            System.out.println(authorOptional.get().getAge());
            System.out.println(authorOptional.get().getId());
        }
    }

    private static void testFilter(){
        Optional<Author> authorOptional = getAuthorOptional();
        authorOptional.filter(author -> author.getAge()>88).ifPresent(author -> System.out.println(author.getName()));
    }
    public static Optional<Author> getAuthorOptional(){
        Author author = new Author(1L,"蒙多",33,"一个从菜刀中明悟哲理的祖安人",null);
        List<Book> books1 = new ArrayList<>();
        books1.add(new Book(1L,"刀的两侧是光明与黑暗","哲学,爱情",88,"用一把刀划分了爱恨"));
        books1.add(new Book(2L,"一个人不能死在同一把刀下","个人成长,爱情",99,"讲述如何从失败中明悟真理"));
        author.setBooks(books1);

        return Optional.ofNullable(author);
    }
    public static Author getAuthor(){
        Author author = new Author(1L,"蒙多",33,"一个从菜刀中明悟哲理的祖安人",null);
        return null;
    }
}
